///////////////////////////////////////////////////////////////////////////////////////////////////
// Illusion Ray Development Kit (IDK)           Copyright (c) 2010 ( dimitriy.martynenko@gmail.com )
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2010-05-22
// Updated : ...
// Licence : NEW BSD
// Info	   : "SCL" Safe Code Lib (for C++ unmanaged)
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once

namespace System
{
	namespace Convert
	{

		template<typename T>
		inline T ToNumber(const String& src)
		{
			T retvalue = (T)0;

			const uint len = src->Length();
			const pwchar chars = src->Chars();
			int fact = 1;
			uint factMul = 1;
			
			for(uint i = 0; i != len; ++i)
			{
				switch(chars[i])
				{
				case '-':
					fact = -1;
					break;
				case '.':
					factMul = 10;
					break;
				default:
					retvalue *= 10;
					retvalue += chars[i]-'0';
					fact *= factMul;
					break;
				}
			}
			return retvalue / fact;
		}

		inline uint ToUInt(const String& src)
		{
			return ToNumber<uint>(src);
		}

		inline float ToFloat(const String& src)
		{
			return ToNumber<float>(src);
		}
		
		inline String ToString(unsigned int value)
		{
			const int maxChars = 10;
			wchar buffer[maxChars + 1];
			buffer[maxChars] = 0; // NULL TERMINANTED

			int i = maxChars;
			do {
				buffer[--i] = value % 10 + '0';
			} while ((value /= 10) > 0);

			return String(&buffer[i]);
		}

		static const int maxChars = 10;
	}
}